package com.dn.designpatterns.abstractfactory;

/**
 * @program: designpatterns
 * @description: 菜鸟教程
 * @author: DN
 * @create: 2021-03-28 20:27
 **/
public abstract class AbstractFactoryC {

    public abstract Color getColor(String color);
    public abstract Shape getShape(String shape);
}


// 形状接口
interface Shape {
    void draw();
}

//
class Rectangle implements Shape {
    public void draw() {
        System.out.println("Inside Rectangle::draw() method.");
    }
}

class Square implements Shape {
    public void draw() {
        System.out.println("Inside Square::draw() method.");
    }
}

class Circle implements Shape {
    public void draw() {
        System.out.println("Inside Circle::draw() method.");
    }
}

interface Color {
    void fill();
}

class Red implements Color {
    public void fill() {
        System.out.println("Inside Red::fill() method.");
    }
}

class Blue implements Color {
    public void fill() {
        System.out.println("Inside Blue::fill() method.");
    }
}

class Green implements Color {
    public void fill() {
        System.out.println("Inside Green::fill() method.");
    }
}

// Shape工厂类
class ShapeFactory extends AbstractFactoryC {
    public Color getColor(String color) {
        return null;
    }

    public Shape getShape(String shape) {
        if (shape == null) {
            return null;
        }
        if (shape.equalsIgnoreCase("CIRCLE")){
            return new Circle();
        } else if (shape.equalsIgnoreCase("RECTANGLE")){
            return new Rectangle();
        } else if (shape.equalsIgnoreCase("SQUARE")){
            return new Square();
        }
        return null;
    }
}

// Color 工厂类
class ColorFactory extends AbstractFactoryC {
    public Color getColor(String color) {
        if (color == null) {
            return null;
        }
        if (color.equalsIgnoreCase("RED")){
            return new Red();
        } else if (color.equalsIgnoreCase("GREEN")){
            return new Green();
        } else if (color.equalsIgnoreCase("BLUE")){
            return new Blue();
        }
        return null;
    }

    public Shape getShape(String shape) {
        return null;
    }
}

// 工厂创造器/生成器类,通过传递形状或颜色信息来获取工厂
class FactoryProducer {
    public static AbstractFactoryC getFactory(String choice){
        if (choice.equalsIgnoreCase("SHAPE")){
            return new ShapeFactory();
        } else if (choice.equalsIgnoreCase("COLOR")){
            return new ColorFactory();
        }
        return null;
    }
}

class AbstractFactoryPatternDemo {
    public static void main(String[] args) {
        // 获取形状工厂
        AbstractFactoryC shapeFactory = FactoryProducer.getFactory("SHAPE");

        // 获取形状为 Circle 的对象
        Shape shape1 = shapeFactory.getShape("CIRCLE");

        //调用 Circle 的 draw 方法
        shape1.draw();

        //获取形状为 Rectangle 的对象
        Shape shape2 = shapeFactory.getShape("RECTANGLE");

        //调用 Rectangle 的 draw 方法
        shape2.draw();

        //获取形状为 Square 的对象
        Shape shape3 = shapeFactory.getShape("SQUARE");

        //调用 Square 的 draw 方法
        shape3.draw();

        //获取颜色工厂
        AbstractFactoryC colorFactory = FactoryProducer.getFactory("COLOR");

        //获取颜色为 Red 的对象
        Color color1 = colorFactory.getColor("RED");

        //调用 Red 的 fill 方法
        color1.fill();

        //获取颜色为 Green 的对象
        Color color2 = colorFactory.getColor("Green");

        //调用 Green 的 fill 方法
        color2.fill();

        //获取颜色为 Blue 的对象
        Color color3 = colorFactory.getColor("BLUE");

        //调用 Blue 的 fill 方法
        color3.fill();
    }
}